﻿using System;
using System.Text;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using Tssipl.PaymentAdapter.Core;
namespace Tssipl.PaymentAdapter.MessageFormat.Sfms
{
    public class SfmsBlock1 : IBlock
    {

        public String AppId { get; set; }
        public char Lt { get; set; }
        public String MessageId { get; set; }
        public String MessageType { get; set; }
        public String SubMessageType { get; set; }
        public String SenderAddress { get; set; }
        public String ReceiverAddress { get; set; }
        public int DeliveryMonitoringFlag { get; set; }
        public int OpenNotificationFlag { get; set; }
        public int NonDeliveryWarningFlag { get; set; }
        public int ObsolescencePeriod { get; set; }
        public String MUR { get; set; }
        public int PossibleDuplicateFlag { get; set; }
        public String ServiceId { get; set; }
        public int OriginatingDate { get; set; }
        public int OriginatingTime { get; set; }
        public int TestingTrainingFlag { get; set; }
        public String SequenceNumber { get; set; }
        public String Filler { get; set; }
        public String UTR { get; set; }
        public int RTGSPriority { get; set; }
        public char BlockEndId { get; set; }

        public SfmsBlock1(String Contents)
        {
            int indexCounter = 0;
            AppId = Contents.Substring(0, 3, ref indexCounter);
            MessageId = Contents.Substring(indexCounter, 3, ref indexCounter);
            Lt = Contents[indexCounter]; indexCounter += 1;
            MessageType = Contents.Substring(indexCounter, 3, ref indexCounter);
            SubMessageType = Contents.Substring(indexCounter, 3, ref indexCounter);
            SenderAddress = Contents.Substring(indexCounter, 11, ref indexCounter);
            ReceiverAddress = Contents.Substring(indexCounter, 11, ref indexCounter);
            DeliveryMonitoringFlag = int.Parse(Contents.Substring(indexCounter, 1, ref indexCounter));
            OpenNotificationFlag = int.Parse(Contents.Substring(indexCounter, 1, ref indexCounter));
            NonDeliveryWarningFlag = int.Parse(Contents.Substring(indexCounter, 1, ref indexCounter));
            ObsolescencePeriod = int.Parse(Contents.Substring(indexCounter, 3, ref indexCounter));
            MUR = Contents.Substring(indexCounter, 16, ref indexCounter);
            PossibleDuplicateFlag = int.Parse(Contents.Substring(indexCounter, 1, ref indexCounter));
            ServiceId = Contents.Substring(indexCounter, 3, ref indexCounter);
            OriginatingDate = int.Parse(Contents.Substring(indexCounter, 8, ref indexCounter));
            OriginatingTime = int.Parse(Contents.Substring(indexCounter, 4, ref indexCounter));
            TestingTrainingFlag = int.Parse(Contents.Substring(indexCounter, 1, ref indexCounter));
            SequenceNumber = Contents.Substring(indexCounter, 9, ref indexCounter);
            Filler = Contents.Substring(indexCounter, 9, ref indexCounter);
            UTR = Contents.Substring(indexCounter, 16, ref indexCounter);
            RTGSPriority = int.Parse(Contents.Substring(indexCounter, 2, ref indexCounter));
        }

        public override String ToString()
        {
            return getFormattedBlock();
        }


        public String getContents()
        {
            StringBuilder sb = new StringBuilder(32);
            sb.Append(AppId);
            sb.Append(MessageId);
            sb.Append(Lt);
            sb.Append(MessageType);
            sb.Append(SubMessageType);
            sb.Append(SenderAddress);
            sb.Append(ReceiverAddress);
            sb.Append(DeliveryMonitoringFlag);
            sb.Append(OpenNotificationFlag);
            sb.Append(NonDeliveryWarningFlag);
            sb.Append(ObsolescencePeriod);
            sb.Append(MUR);
            sb.Append(PossibleDuplicateFlag);
            sb.Append(ServiceId);
            sb.Append(OriginatingDate);
            sb.Append(OriginatingTime);
            sb.Append(TestingTrainingFlag);
            sb.Append(SequenceNumber);
            sb.Append(Filler);
            sb.Append(UTR);
            sb.Append(RTGSPriority);
            return sb.ToString();
        }

        public String getFormattedBlock()
        {
            //HACK StringBuffer sb = new StringBuffer(32);
            StringBuilder sb = new StringBuilder(32);
            sb.Append("{1:");
            sb.Append(getContents());
            sb.Append("}");
            return sb.ToString();
        }
    }
}